# -*- coding: utf-8 -*-
# @File:     process_file.py
# @Author:
# @DateTime: 2025/10/23/11:51
import asyncio
import threading
import uuid

import requests

from app.api.chat.utils.models_manage import get_embeddings
from app.api.chat.utils.verctor_utils.custom_loaders import file_with_loaders_documents
from langchain_milvus import Milvus
from langchain.text_splitter import RecursiveCharacterTextSplitter
from app.conf.settings import *
from app.utils.curd_base import ModelCRUD
from app.models.chat import RAGUploadFile
from app.db.session import SessionLocal


class CRUDRAGUploadFile(ModelCRUD):

    model = RAGUploadFile


def documents_save_milvus(documents, collection_name):

    # 使用LangChain将输入文档安照chunk_size切分
    text_splitter = RecursiveCharacterTextSplitter(chunk_size=1024, chunk_overlap=100)
    # 使用LangChain将输入文档安照chunk_size切分
    all_splits = text_splitter.split_documents(documents)

    embeddings = get_embeddings()

    Milvus.from_documents(
        all_splits,
        embedding=embeddings,
        collection_name=collection_name,
        drop_old=True,
        connection_args={"host": CNF_DATA['milvus']['host'], "port": CNF_DATA['milvus']['port'], 'user': CNF_DATA['milvus']['user'], 'password': CNF_DATA['milvus']['password']},
    )


async def async_process_url_file(process_file_list):
    for file_ins in process_file_list:

        file_byte = requests.get(file_ins['path_url'], timeout=20).content
        db = SessionLocal()
        instance = db.query(RAGUploadFile).filter(RAGUploadFile.id == file_ins['id']).first()
        try:
            docs = file_with_loaders_documents(file_byte, file_ins['name'])
            collection_name = f"file_{uuid.uuid4().hex}"
            documents_save_milvus(docs, collection_name)
            instance.process_state = 3
            instance.vector_uid = collection_name

        except Exception as e:
            print(e)
            instance.process_state = -1
        db.add(instance)
        db.commit()
        db.refresh(instance)


def threading_process_url_file(process_file_list):
    asyncio.run(async_process_url_file(process_file_list))


async def for_url_file_to_milvus(process_file_list):
    # asyncio.create_task(async_process_url_file(process_file_list))
    threading.Thread(target=threading_process_url_file, args=(process_file_list, )).start()








